<template>
  <div class="app-container">
    <el-card class="query-card" v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
        <el-form-item label="歌曲名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入歌曲名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="歌手" prop="artist">
          <el-input
            v-model="queryParams.artist"
            placeholder="请输入歌手"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="是否显示" prop="enable">
          <el-select v-model="queryParams.enable" placeholder="请选择" clearable size="small">
            <el-option
              v-for="dict in dict.type.sys_show_hide"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

    </el-card>
    <el-card class="table-card">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['blog:music:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['blog:music:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['blog:music:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['blog:music:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <vxe-table
        :loading="loading"
        resizable
        :row-config="{isHover: true,height: 90}"
        :data="musicList"
        @checkbox-all="handleSelectionChange"
        @checkbox-change="handleSelectionChange"
        :class="showSearch ? 'table-container' : 'table-container-full' "
      >
        <vxe-column type="checkbox" width="60" fixed="left" align="center"></vxe-column>
        <vxe-column title="序号" align="center" type="seq" min-width="60"/>
        <vxe-column field="cover" title="图片" min-width="100" align="center">
          <template slot-scope="scope">
            <image-preview :key="scope.row.id" v-if="scope.row.cover" :src="scope.row.cover" :width="60" :height="60"/>
          </template>
        </vxe-column>
        <vxe-column title="歌曲名" align="center" field="name" min-width="250"/>
        <vxe-column title="歌手" align="center" field="artist" min-width="180"/>
        <vxe-column title="歌曲链接" align="center" field="url" show-overflow="" min-width="250"/>
        <vxe-column title="是否显示" align="center" field="enable" min-width="150">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_show_hide" :value="scope.row.enable"/>
          </template>
        </vxe-column>
        <vxe-column field="createBy" title="创建人" align="center" min-width="150" show-overflow/>
        <vxe-column field="createTime" title="创建时间" align="center" min-width="160" show-overflow/>
        <vxe-column title="备注" align="center" field="remark" min-width="250"/>
        <vxe-column title="操作" align="center" min-width="230px" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['blog:music:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['blog:music:remove']"
            >删除
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="readLrc(scope.row.lrc)"
            >歌词
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-video-play"
              @click="toListen(scope.row)"
            >播放
            </el-button>
          </template>
        </vxe-column>
      </vxe-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 音乐组件 -->
    <music-player v-show="showSearch" :audio="musicList" :current-music-id="currentMusicId"/>

    <!-- 添加或修改音乐欣赏对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" v-drag-zoom append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="歌名">
              <el-input v-model="form.name"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="歌手">
              <el-input v-model="form.artist"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="歌词">
              <el-input v-model="form.lrc" type="textarea" :rows="20" placeholder="请输入歌词"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="封面">
              <image-upload :visible.sync="open" @setImageUrl="setImageUrl" :limit="1" :file-size="5"
                            file-path="/blog/music-cover" :value="form.cover"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="文件">
              <file-upload :visible.sync="open" @setFileUrl="setFileUrl" :limit="1" :file-size="35"
                           file-path="/blog/music-file" :file-type="musicFileType" :value="form.url"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否显示" prop="enable">
              <el-select v-model="form.enable" placeholder="请选择是否显示">
                <el-option
                  v-for="dict in dict.type.sys_show_hide"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看歌词弹窗 -->
    <el-dialog title="查看歌词" :visible.sync="readLrcDialog" width="500px" v-drag-zoom>
      <div v-html="lrcContent"/>
    </el-dialog>
  </div>
</template>

<script>
import { listMusic, getMusic, delMusic, addMusic, updateMusic } from '@/api/blog/music'

export default {
  name: 'Music',
  dicts: ['sys_show_hide'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 音乐欣赏表格数据
      musicList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        artist: null,
        enable: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 音乐预览
      playInfo: {
        musicUrl: '',
        musicName: ''
      },
      // 自动播放
      autoPlay: false,
      // 控制查看弹窗显示
      readLrcDialog: false,
      // 歌词
      lrcContent: '',
      // 音乐文件类型
      musicFileType: ['mp3', 'wma', 'flac'],
      // 当前播放音乐
      currentMusicId: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询音乐欣赏列表 */
    getList() {
      this.loading = true
      listMusic(this.queryParams).then(response => {
        this.musicList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        artist: null,
        url: null,
        cover: null,
        lrc: null,
        createTime: null,
        enable: null,
        delFlag: null,
        createBy: null,
        updateBy: null,
        remark: null
      }
      this.fileList = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(row) {
      this.ids = row.records.map(item => item.id)
      this.single = row.records.length !== 1
      this.multiple = !row.records.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加音乐'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getMusic(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改音乐'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMusic(this.form).then(response => {
              this.$modal.msgSuccess(response.msg)
              this.open = false
              this.getList()
            })
          } else {
            addMusic(this.form).then(response => {
              this.$modal.msgSuccess(response.msg)
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除音乐欣赏编号为"' + ids + '"的数据项？').then(function() {
        return delMusic(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blog/music/export', {
        ...this.queryParams
      }, `music_${new Date().getTime()}.xlsx`)
    },
    toListen(row) {
      // this.playInfo.musicUrl = row.url
      // this.playInfo.musicName = row.name
      // this.autoPlay = true
      this.currentMusicId = row.id
    },
    readLrc(lrc) {
      // 查看歌词
      if (lrc) {
        this.lrcContent = lrc.replace(new RegExp('\\n', 'gm'), '<br/>')
      }
      this.readLrcDialog = true
    },
    setImageUrl(url) {
      this.form.cover = url
      this.showFileSelect = false
    },
    setFileUrl(url) {
      this.form.url = url
      this.showFileSelect = false
    },
    resetFileUrl(url) {
      if (this.dvCoverSelect) {
        this.setImageUrl(url)
      } else {
        this.setFileUrl(url)
      }
    }
    /* handleSelectFile(flag) {
      if (flag) {
        this.fileExtName = ['png', 'jpg', 'blob', 'jpeg']
      } else {
        this.fileExtName = ['mp3','wma', 'flac']
      }
      this.dvCoverSelect = flag
      this.showFileSelect = true
    } */
  }
}
</script>
<style lang="scss" scoped>

.play-container {
  /* flex布局*/
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
}

.search-holder {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;

  ::v-deep audio {
    -webkit-transition: all 0.5s linear;
    -moz-transition: all 0.5s linear;
    -o-transition: all 0.5s linear;
    transition: all 0.5s linear;
    -moz-box-shadow: 2px 2px 4px 0px #006773;
    -webkit-box-shadow: 2px 2px 4px 0px #006773;
    box-shadow: 2px 2px 4px 0px #006773;
    -moz-border-radius: 7px 7px 7px 7px;
    -webkit-border-radius: 7px 7px 7px 7px;
    border-radius: 7px 7px 7px 7px;
  }
}
</style>
